package com.ts.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ts.common.core.utils.Result;
import com.ts.content.dto.TopicDTO;
import com.ts.content.entity.TopicEntity;
import com.ts.content.entity.UserEntity;
import com.ts.content.mapper.TopicMapper;
import com.ts.content.service.TopicService;
import com.ts.content.service.UserService;
import com.ts.content.vo.Topic2Vo;
import com.ts.content.vo.TopicVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 圈子
 *
 * @author admin admin@qq.com
 * @since 1.0.0 2023-02-22
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, TopicEntity> implements TopicService {
    @Autowired
    private UserService userService;

    @Override
    public IPage getTopicList(IPage<TopicDTO> page) {
        return baseMapper.getTopicList(page);
    }

    @Override
    public IPage<TopicEntity> getUserTopicList(IPage<TopicEntity> page, Integer userId) {
        return baseMapper.getUserTopicList(page,userId);
    }

    @Override
    public List<TopicVo> topicVoListByClassId(Integer classId, Page<TopicEntity> topicPage) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status",0);

        //根据最近时间查询出圈子列表
        if(ObjectUtils.isEmpty(classId)){
            queryWrapper.orderByDesc("create_time");
            IPage<TopicEntity> topicEntityList = baseMapper.selectPage(topicPage, queryWrapper);

            List<TopicVo> topicVoList = getTopicVoList(topicEntityList);

            return topicVoList;
        }

        //根据类目查询圈子列表
        queryWrapper.eq("class_id",classId);

        IPage page = baseMapper.selectPage(topicPage, queryWrapper);
        List topicVoList = getTopicVoList(page);
        return topicVoList;
    }

    //根据id查询帖子信息
    @Override
    public Topic2Vo getUserTopicById(Integer id) {
        TopicEntity topicEntity = baseMapper.selectById(id);

        Topic2Vo topic2Vo = new Topic2Vo();
        BeanUtils.copyProperties(topicEntity,topic2Vo);
        String bgImage = topicEntity.getBgImage();
        if (!ObjectUtils.isEmpty(bgImage)){
            topic2Vo.setBgImage(Arrays.asList(bgImage.split(",")));
        }else {
            topic2Vo.setBgImage(Arrays.asList(bgImage));
        }

        //查询群主的信息
        UserEntity byId = userService.getById(topicEntity.getUserId());
        byId.setCreateTime(null);
        byId.setPhone(null);
        topic2Vo.setUserInfo(byId);

        return topic2Vo;
    }

    private List<TopicVo> getTopicVoList(IPage<TopicEntity> topicEntityList) {
        List<TopicEntity> records = topicEntityList.getRecords();
        //创建一个新的集合
        List<TopicVo> topicVoList = new ArrayList<>();


        //遍历循环拷贝道另一个实体中，再添加道另一个集合中
        for (TopicEntity topicEntity : records) {
            TopicVo topicVo = new TopicVo();
            BeanUtils.copyProperties(topicEntity,topicVo);
            String coverImage = topicEntity.getBgImage();
            if (!ObjectUtils.isEmpty(coverImage)){
                topicVo.setBgImage(Arrays.asList(coverImage.split(",")));
            }else {
                topicVo.setBgImage(Arrays.asList(coverImage));
            }
            topicVoList.add(topicVo);
        }

        return topicVoList;
    }



}